<template lang="html">
  <div>
    <div class="venus-list-page">
      <!--签约查询界面-->
      <div class="page-search">
        <el-form
          label-width="160px"
          style="margin: 20px 50px 20px 0px;"
          size="mini"
          id="form_report_id"
          ref="queryForm"
          :model="form_params"
          :rules="rules"
        >
          <el-row>
            <el-col :span="9">
              <el-form-item label="期初时间:" prop="queryDate">
                <el-date-picker
                  v-model="form_params.queryDate" @focus="hideReport()"
                  type="daterange" value-format="yyyyMMdd" @change="change_date"
                  start-placeholder="开始日期"
                  end-placeholder="结束日期">
                </el-date-picker>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="产品标识类型:" prop="prod_tag_type">
                <el-select v-model="form_params.prod_tag_type"
                           filterable
                           clearable
                           placeholder="请选产品标识类型" @focus="hideReport()">
                  <el-option
                    v-for="(item,val) in dict.prod_tag_type"
                    :key="val"
                    :label="item"
                    :value="val">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item label="产品组合:" prop="t8_sys_portfol_id">
                <el-select v-model="form_params.t8_sys_portfol_id" @change="t8_sys_portfol_id_change_fnc"
                           filterable
                           clearable
                           placeholder="请选产品组合" @focus="hideReport()">
                  <el-option
                    v-for="item in dict.t8_sys_portfol_id"
                    :key="item.value"
                    :label="item.text"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="8">
              <el-form-item label="理财产品:" prop="t8_prod_info_id">
                <el-select v-model="form_params.t8_prod_info_id"
                           filterable
                           clearable
                           placeholder="请选产品组合" @focus="hideReport()">
                  <el-option
                    v-for="item in dict.t8_prod_info_id"
                    :key="item.value"
                    :label="item.text"
                    :value="item.value">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <div style="text-align: center;" >
            <el-button type="success" @click="selectData('queryForm')" size="mini">查询</el-button>
            <el-button type="info" @click="resetForm('queryForm')" size="mini">重置</el-button>
          </div>
        </el-form>
      </div>
    </div>
  </div>
</template>
<script>
  import request from '@/utils/request'
  import * as kk from '@/utils/k-utils'
  export default {
    //name:'index4',
    data() {
      return {
        dict:{
          prod_tag_type:[],
          t8_sys_portfol_id:[],
          t8_prod_info_id:[]
        },
        menuid:'',
        xmladdress:'',
        rules:{
          queryDate:[
            {required: true, message: '请输入查询日期', trigger: 'change'}
          ],
          prod_tag_type:[
            {required: true, message: '请选择产品标识类型', trigger: 'change'}
          ],
          t8_sys_portfol_id:[
            {required: false, message: '请选择产品组合', trigger: 'change'}
          ],
          t8_prod_info_id:[
            {required: false, message: '请选择理财产品', trigger: 'change'}
          ]
        },
        //form_params 表单
        form_params:{
          queryDate:'',             // 查询日期
          start_date:'',            // 开始日期
          end_date:'',              // 结束日期
          prod_tag_type:'',         // 产品标识类型
          t8_sys_portfol_id:'',     // 产品组合
          t8_prod_info_id:''        // 理财产品
        }
      };
    },
    mounted: function() {
      this.menuid = this.$route.meta.menuid;    // 根据菜单参数获取报表ID
      // 加载下拉框
      // 加载投组下拉框
      kk.k_load_exeid('data','M8000EQPORT').then(rep=>{
        this.dict.t8_sys_portfol_id = rep.rows;
      });
      // 加载理财产品下拉框
      kk.k_load_exeid('data','M8000EQPROD').then(rep=>{
        this.dict.t8_prod_info_id = rep.rows;
      });
      // 产品类型的数据字典
      kk.k_load_dict(this,"prod_tag_type","t8_prod_tag_type");
    },

    activated:function(){
      this.showReport();
      setTimeout(() => {
        $("#report_data_id").css("top",($("#form_report_id").height()+130)+"px");
        this.OnReady('AF');
      }, 500);
    },
    deactivated:function(){
      $("#report_data_id").hide();
    },
    methods: {
      change_date() {
        this.form_params.start_date = this.form_params.queryDate[0];
        this.form_params.end_date = this.form_params.queryDate[1];
      },
      t8_sys_portfol_id_change_fnc(){
        this.form_params.t8_prod_info_id = '';
        kk.k_load_exeid('data','M8000EQPROD',{t8_sys_portfol_id:this.form_params.t8_sys_portfol_id}).then(rep=>{
          this.dict.t8_prod_info_id = rep.rows;
        })
      },
      get_random(){
        var d = new Date();
        var str = '';
        str += d.getFullYear();
        str  += d.getMonth() + 1;
        str  += d.getDate();
        str += d.getHours();
        str  += d.getMinutes();
        str+= d.getSeconds();
        var suijishu =	(parseInt(1000*Math.random())).toString();
        var results = str+suijishu;
        return results;
      },
      getRootPath(){
        return kk.k_get_env();
      },
      setXmlUrl(exeid) {
        var str = exeid;
        for ( var key in this.form_params) {
          str += "&" + key + "=" + encodeURI(encodeURI(this.form_params[key]));
        }
        return this.getRootPath()+"/data/getResultData.json?exeid=" + str+"&id="+this.get_random();
      },
      selectData(formName){
        this.$refs[formName].validate((valid) => {
          if (valid) {
            this.showReport();
            setTimeout(() => {
              var rw =[],datas = [],exeids = [];
              kk.k_load_exeid("data","M8430EQ004",{for_table:this.menuid}).then(rep=>{    //加载查询条件的exeid
                if (rep.rows.length > 0) {
                  rw = rep.rows;
                }
                for(var i = 0;i < rw.length;i++){
                  datas.push(rw[i].ds);
                  exeids.push(rw[i].exeid);
                }
                myfunc(AF,"Build", this.xmladdress);
                for(var i=0;i<datas.length;i++){
                  myfunc(AF,"SetSource", datas[i]+" \r\n "+this.setXmlUrl(exeids[i]));
                }
                myfunc(AF,"Calc", "");
                //计算后，重新复制给xmladdress，否则报表隐藏后不会再回显原数据
                this.xmladdress=myfunc(AF,"GetFileXML","isSaveCalculateResult=true");
              });
            }, 500);
          } else {
            this.$message.error("请先输入必填项！");
            return false;
          }
        });
      },
      OnReady(id) {
        if(this.xmladdress == ""){
          request({
            loading: true,
            url: "data/getxml.json",      //查询xml
            method: 'post',
            params: {"for_table":this.menuid}//M812SP005|M84FZ002|M84201|
          }).then(rep =>{
            this.xmladdress=rep.returndata.xml_info;
            myfunc(AF,"Build", this.xmladdress);
            myfunc(AF,"Swkrntpomzqa", "1, 2, 4d, 8, 16, 32, 128");
          });
        }else{
          myfunc(AF,"Build", this.xmladdress);
          myfunc(AF,"Swkrntpomzqa", "1, 2, 4d, 8, 16, 32, 128");
        }
      },
      hideReport(){
        $("#report_data_id").hide();
      },
      showReport(){
        $("#report_data_id").show();
      },
      //重置表单
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    }
  }
</script>

